﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;

public partial class Admin_ucControl_ucListOrder : System.Web.UI.UserControl
{
    TblTypeDAO typeDAO = new TblTypeDAO(Global.CONNECTION_STRING);
    TblBookingDAO bookingDAO = new TblBookingDAO(Global.CONNECTION_STRING);


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadType();
           LoadListOrder();
        }
    }

    private void LoadType()
    {
        drType.DataSource = typeDAO.GetAll();
        drType.DataTextField = "TypeName";
        drType.DataValueField = "IDType";
        drType.DataBind();
    }

    private void LoadListOrder()
    {
        listOrder.DataSource = bookingDAO.GetOrderLst("", Convert.ToInt32(drType.SelectedValue), "");
        listOrder.DataBind();
    }
    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 0: dat hang, 1: da giao hang , 2: huy don hang, 3: Da chuyen tien

        string ItemID = "";
        foreach (ListViewItem lst in listOrder.Items)
        {
            DropDownList drStatus = (DropDownList)lst.FindControl("drStatus");
            HiddenField hid = (HiddenField)lst.FindControl("hid");
            ItemID = hid.Value;
            DataTable dt = null;

            if (drStatus.SelectedValue == "1")
            {
                dt = bookingDAO.Update(1, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "2")
            {
                dt = bookingDAO.Update(2, Global.ConvertToInt(ItemID));
            }
            if (drStatus.SelectedValue == "3")
            {
                dt = bookingDAO.Update(3, Global.ConvertToInt(ItemID));
            }
           
        }

    }
    protected void btnSubmit_Click1(object sender, ImageClickEventArgs e)
    {
        try
        {
            listOrder.DataSource = bookingDAO.GetOrderLst(txtDateEvent.Text, Convert.ToInt32(drType.SelectedValue), drStatus.SelectedValue);
            listOrder.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }

    }
    protected void listOrder_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {

            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            string hid = DataBinder.Eval(dataItem.DataItem, "BookID").ToString();
            HiddenField hid1 = (HiddenField)e.Item.FindControl("hid");
            hid1.Value = hid;

            DataTable Order = new DataTable();
            TblBookingDAO orderDAO = new TblBookingDAO(Global.CONNECTION_STRING);
            Order = orderDAO.GetOrder(Convert.ToInt32(hid));
            DropDownList drStatus = (DropDownList)e.Item.FindControl("drStatus");


            foreach (DataRow dr in Order.Rows)
            {
                if (dr["Status"].ToString() == "1")
                {
                    drStatus.SelectedItem.Text = "Ordered";

                }
                if (dr["Status"].ToString() == "2")
                {
                    drStatus.SelectedItem.Text = "Completed";

                }
                if (dr["Status"].ToString() == "3")
                {
                    drStatus.SelectedItem.Text = "Cancel";
                }
               
            }
        }
    }
}
